package ecode

import (
	"fmt"
	"strconv"
)


// Error new status with code and message
func Error(code Code, message string) *Status {
	return &Status{code: int64(code.Code()), message: message}
}

// Errorf new status with code and message
func Errorf(code Code, format string, args ...interface{}) *Status {
	return Error(code, fmt.Sprintf(format, args...))
}

var _ Codes = &Status{}

type Status struct {
	code     int64
	message  string
}

// Error implement error
func (s *Status) Error() string {
	return s.Message()
}

// Code return error code
func (s *Status) Code() int {
	return int(s.code)
}

// Message return error message for developer
func (s *Status) Message() string {
	if s.message == "" {
		return strconv.Itoa(int(s.code))
	}
	return s.message
}

// Equal for compatible.
// Deprecated: please use ecode.EqualError.
func (s *Status) Equal(err error) bool {
	return EqualError(s, err)
}

// FromCode create status from ecode
func FromCode(code Code) *Status {
	return &Status{code: int64(code)}
}

